var operation = "undefined";
var operation_sign;
var operation_func;
var rank;
var ans = "";
var left_rank;
var right_rank;
var ans_rank;
var temp_ans_rank;
var right_ans = 0;
var right_ans_rank;
var right_score = 0;
var wrong_score = 0;
var randomnumber1 = 0;
var randomnumber2 = 0;
var secCounter = 60;
var isGameOver = 0;
var left_nums = [];
var right_nums = [];
var operations = [];
var repetitions = 0;

function Start() {
	operation_sign = "+";
	operation = "Sum";
	operation_func = newSum11;
	left_rank = 1;
	right_rank = 1;
	ans_rank = -1;
	document.documentElement.addEventListener("keydown", keyHandler, false);
	timer();
	newTask(operation_func);
}

function timer() {
	var timer_watch = document.getElementById("timer1");
	
	timer_watch.firstChild.nodeValue = secCounter.toString();
	if (secCounter != 0) {
		secCounter = secCounter - 1;
		window.setTimeout("timer()", 1000);
	} else {
		var d = document.getElementById("txt1");
		d.firstChild.nodeValue = "Your score is " + right_score.toString();
		isGameOver = 1;
	}
}

function isTaskUniq(number1, number2) {
	for (i = 0; i < left_nums.length; i++)
		if ((number1 == left_nums[i] && number2 == right_nums[i] &&
			operation == operations[i])||(number2 == left_nums[i] &&
			number1 == right_nums[i] && operation == operations[i]) == 1) {
			return 0;
		}
	return 1;
}

function degree(number, deg) {
	var result = number;
	if (deg == 0) return 1;
	for(var i = 1; i < deg; ++i) 
		result *= number;
	return result;
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function max(a, b) {
	if (a > b) return a;
		else return b;
}

function define_rank(num) {
	if (num < 10) return 1;
		else if (num > 9 && num < 100) return 2;
		else if (num > 99 && num < 1000) return 3;
}

function newSum11() {
		randomnumber1 = getRandomInt(1, 9);
		randomnumber2 = getRandomInt(1,9);
		right_ans = randomnumber1 + randomnumber2;
		if (right_ans < 10) temp_ans_rank = 1;
			else temp_ans_rank = 2;
}

function newSum122() {
		randomnumber1 = getRandomInt(1, 9);
		randomnumber2 = getRandomInt(10, 99 - randomnumber1);
		right_ans = randomnumber1 + randomnumber2;
		temp_ans_rank = 2;
}

function newSum222() {
		randomnumber1 = getRandomInt(10, 89);
		randomnumber2 = getRandomInt(10, 99 - randomnumber1);
		right_ans = randomnumber1 + randomnumber2;
		temp_ans_rank = 2;
}

function newSum223() {
		randomnumber1 = getRandomInt(10, 99);
		randomnumber2 = getRandomInt(100 - randomnumber1, 99);
		right_ans = randomnumber1 + randomnumber2;
		temp_ans_rank = 3;
}

function newDif11() {
	randomnumber2 = getRandomInt(1, 9);
	randomnumber1 = getRandomInt(randomnumber2, 9);
	right_ans = randomnumber1 - randomnumber2;
	temp_ans_rank = 1;
}

function newDif21() {
	randomnumber1 = getRandomInt(10, 99);
	randomnumber2 = getRandomInt(1, 9);
	right_ans = randomnumber1 - randomnumber2;
	if (right_ans > 9) temp_ans_rank = 2;
		else temp_ans_rank = 1;
}

function newDif22() {
	randomnumber2 = getRandomInt(10, 99);
	randomnumber1 = getRandomInt(randomnumber2, 99);
	right_ans = randomnumber1 - randomnumber2;
	if (right_ans > 9) temp_ans_rank = 2;
		else temp_ans_rank = 1;
}
	
function newMultiply11() {
	randomnumber1 = getRandomInt(1, 9);
	randomnumber2 = getRandomInt(1, 9);
	right_ans = randomnumber1*randomnumber2;
	if (right_ans > 9) temp_ans_rank = 2;
		else temp_ans_rank = 1;
}

function newMultiply122() {
	randomnumber1 = getRandomInt(1, 9);
	randomnumber2 = getRandomInt(10, Math.floor(99/randomnumber1));
	right_ans = randomnumber1*randomnumber2;
	temp_ans_rank = 2;
}

function newMultiply123() {
	randomnumber1 = getRandomInt(1, 9);
	randomnumber2 = getRandomInt(Math.ceil(100/randomnumber1), 99);
	right_ans = randomnumber1*randomnumber2;
	temp_ans_rank = 3;
}

function newDel21() {
	randomnumber2 = getRandomInt(1, 9);
	randomnumber1 = randomnumber2*getRandomInt(10,
		Math.floor(99/randomnumber2));
	right_ans = randomnumber1/randomnumber2;
	temp_ans_rank = define_rank(right_ans);
}

function newDel31() {
	randomnumber2 = getRandomInt(1, 9);
	randomnumber1 = randomnumber2*
		getRandomInt(Math.ceil(100/randomnumber2), 99);
	right_ans = randomnumber1/randomnumber2;
	temp_ans_rank = define_rank(right_ans);
}

function newSumDif11() {
	var operation_id = getRandomInt(1, 2);
	switch (operation_id) {
		case 1:
			operation = "Sum";
			operation_sign = "+";
			newSum11();
			break;
		case 2:
			operation = "Dif";
			operation_sign = "-";
			newDif11();
			break;
	}
}

function newSumDif21() {
	var operation_id = getRandomInt(1, 2);
	switch (operation_id) {
		case 1:
			operation = "Sum";
			operation_sign = "+";
			newSum122();
			break;
		case 2:
			operation = "Dif";
			operation_sign = "-";
			newDif21();
			break;
	}
}

function newSumDif22() {
	var operation_id = getRandomInt(1, 3);
	switch (operation_id) {
		case 1:
			operation = "Sum";
			operation_sign = "+";
			newSum222();
			break;
		case 2:
			operation = "Dif";
			operation_sign = "-";
			newDif22();
			break;
		case 3:
			operation = "Sum";
			operation_sign = "+";
			newSum223();
			break;
	}
}

function newMulDel1121() {
	var operation_id = getRandomInt(1, 2);
	switch (operation_id) {
		case 1:
			operation = "Multiply";
			operation_sign = "*";
			left_rank = 1;
			newMultiply11();
			break;
		case 2:
			operation = "Del";
			operation_sign = "/";
			left_rank = 2;
			newDel21();
			break;
	}
}

function newMulDel1231() {
	var operation_id = getRandomInt(1, 3);
	switch (operation_id) {
		case 1:
			operation = "Multiply";
			operation_sign = "*";
			left_rank = 1;
			newMultiply122();
			break;
		case 2:
			operation = "Multiply";
			operation_sign = "*";
			left_rank = 1;
			newMultiply123();
			break;
		case 3:
			operation = "Del";
			operation_sign = "/";
			left_rank = 3;
			newDel31();
			break;
	}
}

function newAll12() {	
	var operation_id = getRandomInt(1, 3);
	switch (operation_id) {
		case 1:
			newSumDif11();
			break;
		case 2:
			newSumDif21();
			break;
		case 3:
			newMulDel1121();
			break;
	}
}

function newAll23() {	
	var operation_id = getRandomInt(1, 2);
	switch (operation_id) {
		case 1:
			right_rank = 2;
			newSumDif22();
			break;
		case 2:
			right_rank = 1;
			newMulDel1231();
			break;
	}
}

function take_operation(num1, num2) {
	switch (operation) {
		case "Sum":
			return num1 + num2;
			break;
		case "Dif":
			return num1 - num2;
			break;
		case "Multiply":
			return num1*num2;
			break;
		case "Del":
			return num1/num2;
			break;
	}
}

function take_operation_sign() {
	switch (operation) {
		case "Sum":
			return "+";
			break;
		case "Dif":
			return "-";
			break;
		case "Multiply":
			return "*";
			break;
		case "Del":
			return "/";
			break;
	}
}

function TaskRepeat() {
	if (repetitions == left_nums.length)
		repetitions = 0;
		
	randomnumber1 = left_nums[repetitions];
	randomnumber2 = right_nums[repetitions];
	operation = operations[repetitions];
	operation_sign = take_operation_sign();
	++repetitions;
	
	right_ans = take_operation(randomnumber1, randomnumber2);
	temp_ans_rank = define_rank(right_ans);
}

/*
function AnimateFunc(textElement, AnimateMessage, i) {
	if (i == AnimateMessage.length)
		clearTimeout(intervalID);
		else {
			textElement.firstChild.nodeValue += AnimateMessage[i];
		}
}

function AnimateAppear(textElement, AnimateMessage) {
	var AnimateTextCounter = 0;
	var intervalID = setInterval("AnimateFunc(textElement, 
		AnimateMessage, AnimateTextCounter++); AnimateTextCounter++;", 100);
}		
*/

var NumIterations = 0;
var textElement;
var AnimateMessage;

function AnimateAppear() {
	textElement.firstChild.nodeValue = AnimateMessage.substr(0, NumIterations);

	if (NumIterations < AnimateMessage.length && secCounter >= 1) {
		++NumIterations;
		window.setTimeout("AnimateAppear();", 30);
		//AnimateAppear(textElement, AnimateMessage);
	}
}

function newTask(TaskFunction) {
	var d = document.getElementById("txt1");
	var while_iterations = 0;
	
	if (operation_func != TaskRepeat)
		do {
			while_iterations++;
			TaskFunction();
			if (while_iterations > 100) {
				operation_func = TaskRepeat;
				break;
			}
		} while (isTaskUniq(randomnumber1, randomnumber2) == 0);
	if (operation_func == TaskRepeat) TaskRepeat();
	
	d.firstChild.nodeValue = "";
	var TaskMessage = randomnumber1.toString() + 
		" " + operation_sign + " " + randomnumber2.toString() + " = ";
	NumIterations = 0;
	
	AnimateMessage = TaskMessage;
	textElement = d;
	AnimateAppear();
	//alert(TaskMessage);
		/*
	d.firstChild.nodeValue = randomnumber1.toString() + 
		" " + operation_sign + " " + randomnumber2.toString() + " = ";
		*/
	left_nums[left_nums.length] = randomnumber1;
	right_nums[right_nums.length] = randomnumber2;
	operations[operations.length] = operation;
}

function right_code(rightCode) {
	if (rightCode > 95 && rightCode < 106)
		rightCode -= 48;
	//alert(keyCode);
	return rightCode;
}

function keyHandler(event) {
  event.preventDefault(); 
  if (isGameOver == 1) return;
  var d = document.getElementById("txt1");
  if (ans.length < temp_ans_rank - 1) {
	ans = ans + String.fromCharCode(right_code(event.keyCode));
	d.firstChild.nodeValue += String.fromCharCode(right_code(event.keyCode));
	} 
	else if (ans.length == temp_ans_rank - 1) {
		ans = ans + String.fromCharCode(right_code(event.keyCode));
		if (ans == right_ans.toString()) {
			ans = "";
			right_score++;
			document.body.style.background = "green";
			window.setTimeout("document.body.style.background = 'white';", 100);
			d.firstChild.nodeValue += String.fromCharCode(right_code(event.keyCode));
			window.setTimeout("newTask(operation_func);", 100);			
		} else {
			ans = "";
			wrong_score++;
			document.body.style.background = "red";
			window.setTimeout("document.body.style.background = 'white';", 100);
			d.firstChild.nodeValue += String.fromCharCode(right_code(event.keyCode));
			window.setTimeout("newTask(operation_func);", 100);
		}
	}
}